AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("sh_player.lua")
AddCSLuaFile("cl_gui.lua")

include("shared.lua")
include("sh_player.lua")

local CommRedistribute = { 0.0, 0.0 }



timer.Create( "ResourceGive", 1.0, 0, function()

	local ResNodes = ents.FindByClass( "garc_resnode" )
	local ResToGive = {0,0}
	
	for _, v in pairs( ResNodes ) do
	
		if ResToGive[ v.Team ] then			
			ResToGive[ v.Team ] = ResToGive[ v.Team ] + 1
		end
	
	end
	
	for k, v in pairs( CommRedistribute ) do
		
		local Comm = team.GetComm( k )
		
		if IsValid( Comm ) then
			Comm:AddResource( ResToGive[ k ] * (1-v) )
			RESOURCE:Distribute( k, ResToGive[ k ] * v )
		else
			RESOURCE:Distribute( k, ResToGive[ k ] ) //No comm, distribute res evenly
		end
		
	end

end ) 

function RESOURCE:Distribute( Team, amount )

	if amount == 0 then
		return
	end

	local Players = team.GetPlayers( Team )
	local PerUser = amount / #Players
	
	for _, v in pairs( Players ) do
		v:AddResource( PerUser )
	end

end